<?php


namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use App\Mongodb;

class PhpinfoController extends Controller
{
    public function phpinfo() {
        echo phpinfo();
    }

    //写入mongodb
    public function adddata()
    {

    }
    public function mongoList(){
        $model = DB::connection('mongodb')->collection('cmc_comment');
        //链接mongodb的test集合
        $res = $model->get(); #获取集合所有文档
        dd($res);
    }
    //添加
    public function mongoAdd(){
        $model = DB::connection('mongodb')->collection('cmc_comment');
        //链接mongodb的test集合
        $data = ['name'=>'杨宝源'.mt_rand(1,10),'age'=>mt_rand(1,10)];
        $res = $model->insert($data);//插入集合文档，数组形式传递参数
        dd($res);
    }
    //编辑
    public function mongoEdit(){
        $model = DB::connection('mongodb')->collection('cmc_comment');
        //链接mongodb的test集合
        $data = ['name'=>'杨宝源121','age'=>90];
        $where = ['age'=>11];
        $res = $model->update(['age' => 100],
            ['$set'=>['name' => '杨宝源']]);
        //更新集合文档，数组形式传递参数
        dd($res);
    }
    //删除
    public function mongoDel(){
        $model = DB::connection('mongodb')->collection('cmc_comment');
        //链接mongodb的test集合
        $res = $model->where(['age'=>100])->delete();
        //根据条件删除文档
        dd($res);
    }
    public function demo()
    {
        /*$connection = Mongodb::connectionMongodb('cmc_comment');
        $data = ['name'=>'aaa','age'=>21];
        $result= $connection ->insert($data);
        dd($result);*/
        $data = ['$rename'=>['age'=>'ageage']];
        $connection = Mongodb::connectionMongodb('cmc_comment');
        $result= $connection ->where('age',22)->update($data);
        dd($result);
    }

}